const app = getApp();
const imgBaseUrl = app.globalData.imgBaseUrl;
Page({
  data: {
    imgBaseUrl: imgBaseUrl,
    vip: ['您还不是会员', '月会员', '季会员', '年会员'],
    name: '',
    windowHeight: app.globalData.windowHeight
  },
  onShow: function () {
    var isLogin = app.isLogin();
    console.log(app.globalData.windowHeight);
    this.setData({
      isLogin: isLogin
    })
    if (isLogin) {
      this.getUser();
    }
  },
  getUser: function () {
    var that = this;
    app.func.postReq('/text/index.php/home/user/userinfo', {}, function (res) {
      that.setData({
        grade: res.user.grade,
        name: res.user.nickname,
        avatar: res.user.avatar,
        vip_end: res.user.vip_end
      })
    })
  },
  getUserInfo: function (e) {
    var that = this;
    var data = {};
    if (e.detail.errMsg == "getUserInfo:ok") {
      that.login(e.detail.userInfo, e);
    }
  },
  login: function (data, e) {
    var that = this;
    wx.login({
      success: res => {
        data.code = res.code;
        app.func.postReq('/text/index.php/home/login/adduser', data, function (res) {
          if (res) {
            wx.setStorageSync('hhh', res.users.id)
            that.setData({
              isLogin: 1
            })
            that.getUser();
            app.func.postReq('/text/index.php/home/shopcar/cars', {}, function (res) {
              if (parseFloat(res) > 0) {
                wx.setTabBarBadge({
                  index: 2,
                  text: res.toString(),
                })
              }
            })
          }
        })
      }
    })
  },
  toOpenVip: function () {
    if (this.data.grade == 0) {
      wx.navigateTo({
        url: '../openvip/openvip',
      })
    }
  }
})